home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / echotcp.rexx < prev    next >
OS/2 REXX Batch file  |  2000-11-28  |  1KB  |  49 lines

  1. /*
  2.     A very simple echo tcp client.
  3.     Show how to make a basic connection to a tcp service.
  4.     To test it on localhost, be sure echo/tcp is enabeld
  5.     in the services and inetd database, then write
  6.     rx echotcp localhost.
  7. */
  8.  
  9. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  10. if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then exit
  11.  
  12. prg = ProgramName("NOEXT")
  13.  
  14. if ~RMH_ReadArgs("HOST/A") then do
  15.     call PrintFault(IoErr(),prg)
  16.     exit
  17. end
  18.  
  19. addr = resolve(parm.0.value)
  20. if addr=="-1" then call err "no host <"parm.0.value">"
  21.  
  22. if ~getservbyname("SE","echo","tcp") then
  23.     call err "echo tcp service not found"
  24.  
  25. sin.ADDRFAMILY = "INET"
  26. sin.ADDRADDR   = addr
  27. sin.ADDRPORT   = SE.SERVPORT
  28.  
  29. sock = socket("INET","STREAM","IP")
  30. if sock<0 then call err "no socket ("errno()")"
  31.  
  32. if connect(sock,"SIN")<0 then call err "connect error ("errno()")"
  33.  
  34. REQUEST = "echo service test"
  35. res = send(sock,REQUEST)
  36. if res~=length(REQUEST) then call err "send error ("errno()")"
  37.  
  38. len = recv(sock,"BUF",256)
  39. if len<0 then call err "recv error ("errno()")"
  40.  
  41. say buf
  42. call CloseSocket(sock)
  43. exit
  44.  
  45. err: procedure expose prg
  46. parse arg msg
  47.     say prg":" msg
  48.     exit
  49.